package com.zerro.litez.runtime;


public abstract class LitEzDatabase {

    public LitEzSupporter supporter;

    private static LitEzDatabase instance = null;


    protected LitEzDatabase() {
        this.supporter = new LitEzSupporter();

    }

    /**
     * 创建数据库
     *
     * @return 数据库实例
     */

    public static LitEzDatabase build() {
        if (instance == null) {
            try {
                Class<?> klass = Class.forName(
                        "com.zerro.litez_orm.on_track.LitEzDatabase_Imp");
                instance = (LitEzDatabase) klass.getConstructor()
                        .newInstance();
            } catch (Exception | Error e) {
                if (e instanceof NoClassDefFoundError) {
                    throw new RuntimeException("无法找到类：" +
                            e.getMessage().replaceAll("/", ".") +
                            "，请删除 编译 文件夹（通常是 target）并重新构建项目");
                }
                throw new RuntimeException(e);
            }
        }
        return instance;

    }



    public static LitEzDatabase getInstance() {
        if (instance == null) {
            throw new RuntimeException("数据库还未创建");
        }
        return instance;
    }

    /**
     * 根据传入的Dao.class访问相应的Dao
     *
     * @param dao 想要访问的Dao
     * @return Dao实例
     */

    public abstract <T> T access(Class<T> dao);

}
